<template>
  <div class="flex flex-col h-full">
    <SearchContainer @search="onSearch" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" class="form-style-flex" :model="formState" :label-col="{ style: { width: '70px' } }">
        <a-form-item name="dateRange" label="收费日期" class="w-80 mr-5">
          <a-range-picker v-model:value="formState['dateRange']" format="YYYY-MM-DD" value-format="YYYY-MM-DD" @change="getTollCollectorData" />
        </a-form-item>
        <a-form-item name="pyjm" label="" class="w-64">
          <a-input v-model:value="formState.pyjm" placeholder="姓名/助记码/卡号/手机号/身份证号" />
        </a-form-item>
        <a-form-item name="pjhm" class="w-64 ml-5">
          <a-input v-model:value="formState.pjhm" placeholder="票据号" />
        </a-form-item>
        <a-form-item name="jslxbzList" label="结算类型" class="w-72 ml-3">
          <a-select v-model:value="formState.jslxbzList" mode="multiple" placeholder="请选择结算类型" allow-clear>
            <a-select-option value="0">门诊押金</a-select-option>
            <a-select-option value="1">挂号收费</a-select-option>
            <a-select-option value="2">门诊收费</a-select-option>
            <!-- <a-select-option value="7">视光押金</a-select-option>
            <a-select-option value="8">视光收费</a-select-option> -->
          </a-select>
        </a-form-item>
        <a-form-item label="收费员" name="czy" class="w-72">
          <a-select v-model:value="formState.czy" mode="multiple" :max-tag-count="2" placeholder="请选择" :options="wardData" />
        </a-form-item>
        <a-form-item name="isTf" label="显示退费" class="w-32 ml-5">
          <a-switch v-model:checked="formState.isTf" title="显示退费" un-checked-value="0" checked-value="KQ" @change="onSearch" />
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2 flex-1">
      <div class="flex justify-end mb-2">
        <BaseExport type="门诊" name="收费检索" code="00001171" :search-params="formState">导出</BaseExport>
      </div>
      <BaseTable
        fill
        :loading="loading"
        column-code="00001171"
        :scroll="{ x: 1850 }"
        :data-source="tableData"
        bordered
        :pagination="pagination"
        :extra-height="45"
      >
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'action'">
            <a-button type="link" size="small" @click="onHandleEdit(record)"> 明细 </a-button>
            <a-divider type="vertical" />
            <a-button type="link" size="small" @click="onPrint(record)"> 补打收据 </a-button>
            <a-divider type="vertical" />
            <a-button v-authority type="link" size="small" @click="onPrintInvoice(record)"> 发票 </a-button>
          </template>
        </template>
        <template #summary>
          <a-table-summary fixed>
            <a-table-summary-row>
              <template v-for="column in columnsData" :key="column.dataIndex">
                <a-table-summary-cell v-if="column.dataIndex === 'xh'"> 合计 </a-table-summary-cell>
                <a-table-summary-cell v-else-if="column.dataIndex === 'ysje'">
                  <div class="text-right">{{ formatAmount(formTotalSum.ysje) }}</div>
                </a-table-summary-cell>
                <a-table-summary-cell v-else-if="column.dataIndex === 'yhje'">
                  <div class="text-right">{{ formatAmount(formTotalSum.yhje) }}</div>
                </a-table-summary-cell>
                <a-table-summary-cell v-else-if="column.dataIndex === 'ybje'">
                  <div class="text-right">{{ formatAmount(formTotalSum.ybje) }}</div>
                </a-table-summary-cell>
                <a-table-summary-cell v-else-if="column.dataIndex === 'zfje'">
                  <div class="text-right">{{ formatAmount(formTotalSum.zfje) }}</div>
                </a-table-summary-cell>
                <a-table-summary-cell v-else-if="column.dataIndex === 'dyqzf'">
                  <div class="text-right">{{ formatAmount(formTotalSum.dyqzf) }}</div>
                </a-table-summary-cell>
                <a-table-summary-cell v-else-if="column.dataIndex === 'vipzf'">
                  <div class="text-right">{{ formatAmount(formTotalSum.vipzf) }}</div>
                </a-table-summary-cell>
                <a-table-summary-cell v-else-if="column.dataIndex === 'fbyh'">
                  <div class="text-right">{{ formatAmount(formTotalSum.fbyh) }}</div>
                </a-table-summary-cell>
                <a-table-summary-cell v-else-if="column.dataIndex === 'yjzf'">
                  <div class="text-right">{{ formatAmount(formTotalSum.yjzf) }}</div>
                </a-table-summary-cell>
                <a-table-summary-cell v-else-if="column.dataIndex === 'wechatInput'">
                  <div class="text-right">{{ formatAmount(formTotalSum.wechatInput) }}</div>
                </a-table-summary-cell>
                <a-table-summary-cell v-else-if="column.dataIndex === 'wxzf'">
                  <div class="text-right">{{ formatAmount(formTotalSum.wxzf) }}</div>
                </a-table-summary-cell>
                <a-table-summary-cell v-else-if="column.dataIndex === 'zfbzf'">
                  <div class="text-right">{{ formatAmount(formTotalSum.zfbzf) }}</div>
                </a-table-summary-cell>
                <a-table-summary-cell v-else-if="column.dataIndex === 'wechatpayapp'">
                  <div class="text-right">{{ formatAmount(formTotalSum.wechatpayapp) }}</div>
                </a-table-summary-cell>
                <a-table-summary-cell v-else-if="column.dataIndex === 'wechatpayscan'">
                  <div class="text-right">{{ formatAmount(formTotalSum.wechatpayscan) }}</div>
                </a-table-summary-cell>
                <a-table-summary-cell v-else-if="column.dataIndex === 'gzzf'">
                  <div class="text-right">{{ formatAmount(formTotalSum.gzzf) }}</div>
                </a-table-summary-cell>
                <a-table-summary-cell v-else-if="column.dataIndex === 'xykzf'">
                  <div class="text-right">{{ formatAmount(formTotalSum.xykzf) }}</div>
                </a-table-summary-cell>
                <a-table-summary-cell v-else-if="column.dataIndex === 'cxkzf'">
                  <div class="text-right">{{ formatAmount(formTotalSum.cxkzf) }}</div>
                </a-table-summary-cell>
                <a-table-summary-cell v-else-if="column.dataIndex === 'zffs01'">
                  <div class="text-right">{{ formatAmount(formTotalSum.zffs01) }}</div>
                </a-table-summary-cell>
                <a-table-summary-cell v-else-if="column.dataIndex === 'zffs02'">
                  <div class="text-right">{{ formatAmount(formTotalSum.zffs02) }}</div>
                </a-table-summary-cell>
                <a-table-summary-cell v-else-if="column.dataIndex === 'zffs03'">
                  <div class="text-right">{{ formatAmount(formTotalSum.zffs03) }}</div>
                </a-table-summary-cell>
                <a-table-summary-cell v-else-if="column.dataIndex === 'zffs04'">
                  <div class="text-right">{{ formatAmount(formTotalSum.zffs04) }}</div>
                </a-table-summary-cell>
                <a-table-summary-cell v-else-if="column.dataIndex === 'zffs05'">
                  <div class="text-right">{{ formatAmount(formTotalSum.zffs05) }}</div>
                </a-table-summary-cell>
                <a-table-summary-cell v-else-if="column.dataIndex === 'zffs06'">
                  <div class="text-right">{{ formatAmount(formTotalSum.zffs06) }}</div>
                </a-table-summary-cell>
                <a-table-summary-cell v-else-if="column.dataIndex === 'zffs07'">
                  <div class="text-right">{{ formatAmount(formTotalSum.zffs07) }}</div>
                </a-table-summary-cell>
                <a-table-summary-cell v-else-if="column.dataIndex === 'zffs08'">
                  <div class="text-right">{{ formatAmount(formTotalSum.zffs08) }}</div>
                </a-table-summary-cell>
                <a-table-summary-cell v-else-if="column.dataIndex === 'zffs09'">
                  <div class="text-right">{{ formatAmount(formTotalSum.zffs09) }}</div>
                </a-table-summary-cell>
                <a-table-summary-cell v-else-if="column.dataIndex === 'zffs10'">
                  <div class="text-right">{{ formatAmount(formTotalSum.zffs10) }}</div>
                </a-table-summary-cell>
                <a-table-summary-cell v-else-if="column.dataIndex === 'xjzf'">
                  <div class="text-right">{{ formatAmount(formTotalSum.xjzf) }}</div>
                </a-table-summary-cell>
                <a-table-summary-cell v-else> - </a-table-summary-cell>
              </template>
            </a-table-summary-row>
          </a-table-summary>
        </template>
      </BaseTable>
    </a-card>
    <Edit v-model:open="editOpen" :row-data="rowData" @success="onSuccess" />
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import dayjs from 'dayjs'
import { message } from 'ant-design-vue'
import SearchContainer from '@/components/SearchContainer'
import { post } from '@/utils/request'
import { getSystemConfig } from '@/utils/systemConfig'
import { formatAmount } from '@/utils/format.js'
import Edit from './Edit.vue'

const rowData = ref({})
const editOpen = ref(false)
const formRef = ref(null)
const formTotalSum = ref({})
const formState = ref({
  dateRange: [dayjs().format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')],
  isTf: '0'
})
// /outp/oupCharge/getTollRetrievalListSum  合计接口
const { loading, tableData, pagination, getData } = useTable({
  url: '/outp/oupCharge/getTollRetrievalList',
  params: formState.value
})

// 合计
const getTotalSum = async () => {
  const { code, data, msg } = await post('/outp/oupCharge/getTollRetrievalListSum', {
    ...formState.value,
    stdate: formState.value.dateRange?.[0],
    eddate: formState.value.dateRange?.[1]
  })
  if (code !== 0) return message.error(msg)
  if (data) {
    formTotalSum.value = data
  }
}

const onHandleEdit = (row) => {
  rowData.value = { ...row }
  editOpen.value = true
}

const onPrint = async (row) => {
  if (row.jslxbz === 0) {
    // 押金收费打印
    const { code, msg, data } = await post('/outp/outPrint/getDepositSlip', { lsh: row?.keycode, type: 1 })
    if (code !== 0) return message.error(msg)
    window.openExe?.rpt(data)
  }
  if (row.jslxbz === 1) {
    // 挂号收费打印
    const url = getSystemConfig('REGISTRATION_FORM_PRINT_MODE') === '2' ? '/outp/outPrint/getRegistrationPrint' : '/outp/outPrint/getRegistrationBillPrint' // 0：通用|1：上海黑马|2：茗视光
    const { code, msg, data } = await post(url, { jzbh: row.jzbh, pjhm: row.pjhm || null, sftfbs: 0 })
    if (code !== 0) return message.error(msg)
    window.openExe?.rpt(data)
  }
  if (row.jslxbz === 2 || row.jslxbz === 8) {
    // 门诊收费打印
    const { code, msg, data } = await post('/outp/oupCharge/PrintMedicareCharges', { jzbh: row?.jzbh, pjhm: row.pjhm })
    if (code !== 0) return message.error(msg)
    window.openExe?.rpt(data)
  }
}
const wardData = ref([])
// 收费员
const getTollCollectorData = async () => {
  const res = await post('/outp/mzdj/getTollCollectors', {
    stdate: formState.value['dateRange']?.[0],
    eddate: formState.value['dateRange']?.[1]
  })
  wardData.value = res.data.map((item) => {
    return {
      label: item.empName,
      value: item.empId
    }
  })
  onSearch()
}

// 打印发票
const onPrintInvoice = async (row) => {
  const { code, msg, data } = await post('/outp/oupCharge/printMedicareChargesFP', { jzbh: row.jzbh, pjhm: row.pjhm })
  if (code !== 0) return message.error(msg)
  // OUTP_REGISTER_INVOICE_MODE 收费发票样式 0：通用|1：全电发票|2：福柏套打
  if (getSystemConfig('OUTP_REGISTER_INVOICE_MODE') === '1') {
    window.open(data)
  } else if (getSystemConfig('OUTP_REGISTER_INVOICE_MODE') === '2') {
    data && window.openExe?.rpt(data)
  }
}

//获取表头
const columnsData = ref([])
const getTableColumns = async () => {
  const { code, data } = await post('/common/util/getTableMappingCloumnList', { id: '00001171' })
  if (code !== 0) return message.error(code)
  columnsData.value = data.filter((item) => {
    item.width = parseToNumber(item.width)
    return !data.includes(item.dataIndex) && !item.isHide
  })
}
function parseToNumber(str) {
  const num = parseFloat(str)
  if (!isNaN(num)) {
    return num
  } else {
    return undefined
  }
}

const onSearch = () => {
  getData()
  getTotalSum()
}

const onSuccess = () => {
  editOpen.value = false
  onSearch()
}
onMounted(() => {
  getData()
  getTollCollectorData()
  getTableColumns()
  getTotalSum()
})
</script>
<style lang="less" scoped>
.ant-picker {
  width: 100%;
}
</style>
